package main

import (
	"io/ioutil"
	"fmt"
)

func copy(srcFileName string, dstFileName string) (err error) {
	byteStr, err := ioutil.ReadFile(srcFileName)
	if err!= nil {
		fmt.Println(err)
		return err
	}
	err2 := ioutil.WriteFile(dstFileName, byteStr, 006)
	if err2!= nil {
		fmt.Println(err2)
		return err2
	}
	return nil
}

func CopyFile(srcFileName string, dstFileName string) (err error) {
	sFile, err1 := os.Open(srcFileName)
	defer sFile.Close()
	sFile1, err2 := os.Open(dstFileName, os.O_WRONLY|os.O_CREATE, 0666)
	defer sFile1.Close()
	if err1!= nil {
		fmt.Println(err1)
		return err1
	}
	if err2!= nil {
		fmt.Println(err2)
		return err2
	}

	var tempSlice = make([]byte, 128)
	for {
		n, err1 := sFile.Read(tempSlice)
		if err1 != nil {
			if err1 == io.EOF {
				break
			}
			fmt.Println(err1)
			return err1
		}
		n2, err2 = sFile1.Write(tempSlice[:n])
		if err2 != nil {
			fmt.Println(err2)
			return err2
		}
	}
	
	return nil
}

func main() {
	err := copy("./hello.txt", "./test_copy.txt")
	if err!= nil {
		fmt.Println(err)
		return
	}
	fmt.Println("文件复制成功！")
}